﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.WebApp._console.core
{
    public partial class user_edit : System.Web.UI.Page
    {
        private Guid _applicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _applicationId = new Guid(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                if (_applicationId != Guid.Empty)
                {
                    User user = service.GetUser(_applicationId);
                    if (user != null)
                    {
                        _ctlEditName.Text = user.Name;
                        _ctlEditEmail.Text = user.Email;
                        _ctlEditPhone.Text = user.Telphone;
                        _ctlEditAccount.Text = user.Account;
                    }
                }

                _ctlLinkBack.NavigateUrl = "./user-list.aspx";
            }
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();

            User user = service.GetUser(_applicationId);
            if (user != null)
            {
                user.Name = _ctlEditName.Text;
                user.Account = _ctlEditAccount.Text;
                user.Email = _ctlEditEmail.Text;
                user.Telphone = _ctlEditPhone.Text;

                if (service.SaveUser(user))
                {
                    Response.Redirect(_ctlLinkBack.NavigateUrl);
                }
            }
        }
    }
}